import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'

import AutoImport from 'unplugin-auto-import/vite' // 自动导入配置的模块
import Components from 'unplugin-vue-components/vite' // 自动导入组件的模块
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' // 自动导入 Element Plus 的 Api
import ElementPlus from 'unplugin-element-plus/vite' // 自动导入 Element Plus 的 style

const host = process.env.TAURI_DEV_HOST

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  base: './',
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
      dts: 'types/auto-generate/auto-import.d.ts'
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: 'types/auto-generate/components.d.ts'
    }),
    ElementPlus({})
  ],
  clearScreen: false,
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler'
      }
    }
  },
  server: {
    proxy: {
      '/cloudGame': {
        // target: 'https://api.shengsuan.com',
        target: 'http://192.168.137.8',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/cloudGame/, '') // 重写请求路径，去掉 '/game' 前缀
      }
    },
    port: 1420,
    strictPort: true,
    host: host || false,
    hmr: host
      ? {
          protocol: 'ws',
          host,
          port: 1420
        }
      : undefined
  },
  build: {
    outDir: 'app_client/dist'
  }
}))
